﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ProgressBarDefinition.h"
#include "ControlDefinition.h"
#include "GbmpNew.h"
#include "ControlDefinitionChangeEvent.h"
#include "IProgressBarHandler.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

namespace gcmp
{

ProgressBarDefinition::ProgressBarDefinition(const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip, int length)
    : m_controlDefinition(NEW_AS_OWNER_PTR(ControlDefinition, id, caption, tooltip))
    , m_value(0)
    , m_textVisible(true)
    , m_width(length)  
{
    m_pProgressBarDefinitionChangeEvent = NEW_AS_OWNER_PTR(ControlDefinitionChangeEvent, L"ControlDefinitionChangeEvent", this);
}

std::wstring ProgressBarDefinition::GetId() const
{
    return m_controlDefinition->GetId();
}

std::wstring ProgressBarDefinition::GetCaption() const
{
    return m_controlDefinition->GetCaption();
}

std::wstring ProgressBarDefinition::GetTooltip() const
{
    return m_controlDefinition->GetTooltip();
}

int ProgressBarDefinition::GetValue() const
{
    return m_value;
}

bool ProgressBarDefinition::SetValue(int value)
{
    if (value != m_value)
    {
        m_value = value;
        EmitDefineChangeEvent();
    }
    return true;
}

bool ProgressBarDefinition::GetTextVisible() const
{
    return m_textVisible;
}

bool ProgressBarDefinition::SetTextVisible(bool visible)
{
    if (visible != m_textVisible)
    {
        m_textVisible = visible;
        EmitDefineChangeEvent();
    }
    return true;
}

bool ProgressBarDefinition::SetWidth(int width)
{
    if (m_width != width)
    {
        m_width = width;
        EmitDefineChangeEvent();
    }
    return true;
}

int ProgressBarDefinition::GetWidth() const
{
    return m_width;
}

IControlDefinitionChangeEvent* ProgressBarDefinition::GetControlDefinitionChangeEvent() const
{
    return const_cast<ControlDefinitionChangeEvent*>(m_pProgressBarDefinitionChangeEvent.get());
}

bool ProgressBarDefinition::IsVisible() const
{
    if (m_pHandler)
    {
        return m_pHandler->IsVisible();
    }
    return m_controlDefinition->IsVisible();
}

bool ProgressBarDefinition::IsEnabled() const
{
    return m_controlDefinition->IsEnabled();
}

bool ProgressBarDefinition::SetVisible(bool visible)
{
    bool res = m_controlDefinition->SetVisible(visible);
    if (res)
    {
        EmitDefineChangeEvent();
    }
    return res;
}

bool ProgressBarDefinition::SetControlHandler(OwnerPtr<IProgressBarHandler> pControlHandler)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pControlHandler, L"pControlHandler为空",L"GDMPLab",L"2024-03-30");
    bool visible = IsVisible();
    bool enabled = IsEnabled();
    IProgressBarHandler* pHandler = pControlHandler.get();
    m_pHandler = TransferOwnership(pControlHandler);

    if (visible != pHandler->IsVisible())
    {
        EmitDefineChangeEvent();
    }
    return true;
}

void ProgressBarDefinition::EmitDefineChangeEvent()
{
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
    m_pProgressBarDefinitionChangeEvent->Emit(&arguments);
}

}   // end namespace gcmp
